import React from 'react';
import {Row, Col} from 'antd'
import './index.scss'
import Util from './../../../assets/js/utils'
import Axios from './../../../assets/js/axios'
import {connect} from 'react-redux';
import {switchTitle}   from './../../redux/action'
import {withRouter} from "react-router-dom";

class Header extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
        }
    }
    componentDidMount() {
        this.mounted = true;
        let headerTitle = window.sessionStorage.getItem('headerTitle')
        const {dispatch} = this.props;
        dispatch(switchTitle(headerTitle))
        setInterval(() => {
            let sysTime = Util.formateDate(new Date().getTime());
            if(this.mounted) {
                this.setState({
                    sysTime
                })
            }
        }, 1000)
        this.getWeatherAPIData()
    }

    getWeatherAPIData() {
        let city = '南通'
        Axios.jsonp({
            url: 'http://api.map.baidu.com/telematics/v3/weather?location='+encodeURIComponent(city)+'&output=json&ak=3p49MVra6urFRGOT9s8UBWr2'
        }).then ((res) => {
            if(res.status === 'success') {
                let data = res.results[0].weather_data[0];
                if(this.mounted) {
                    this.setState({
                        dayPictureUrl: data.dayPictureUrl,
                        weather: data.weather
                    })
                }
            }
        })
    }

    loginOut = () => {
        Axios.ajax({
            url: '/api/users/signOut',
            methods: 'POST'
        }).then((res) => {
            if(res.code === '000000') {
                window.sessionStorage.clear()
                this.props.history.push('/login')
            }
        })
    }

    componentWillUnmount() {
        this.mounted = false;
    }

    render() {
        const menuType = this.props.menuType;
        return (
            <div className="header">
                <Row className="header-top">
                    {
                        menuType?
                            <Col span="6" className="logo">
                                <img src="/assets/logo-ant.svg" alt=""/>
                                <span>IMooc 通用管理系统</span>
                            </Col>: ''
                    }
                    <Col span={menuType?18:24}>
                        <span>欢迎，{this.props.title}</span>
                        <span className="login_out" onClick={this.loginOut}>退出</span>
                    </Col>
                </Row>
                {
                    menuType?'':
                        <Row className="breadcrumb">
                            <Col span="4" className="breadcrumb-title">
                                {this.props.menuName ? this.props.menuName : '首页'}
                            </Col>
                            <Col span="20" className="weather">
                                <span className="date">{this.state.sysTime}</span>
                                <span className="weather-img">
                                    <img src={this.state.dayPictureUrl} alt=""/>
                                </span>
                                <span className="weather-detail">
                                    {this.state.weather}
                                </span>
                            </Col>
                        </Row>
                }
            </div>
        );
    }
}
const mapStateToProps = state => {
    return {
        menuName: state.menuName,
        title: state.title
    }
}
export default connect(mapStateToProps)(withRouter(Header));